﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Configurations.Controller;

namespace Pixysoft.Framework.Configurations.Core
{
    class HtmlDiv : IHtmlDiv
    {
        string preText;

        string parentpath;

        int nestLevel = 0;

        int divIndex = 0;

        IHtmlNode node;

        public HtmlDiv(IHtmlNode node, string parentpath, int nestLevel, int divIndex)
        {
            this.parentpath = parentpath;
            this.nestLevel = nestLevel;
            this.divIndex = divIndex;
            this.node = node;
        }

        public HtmlDiv(IHtmlNode node, string parentpath, int nestLevel, int divIndex, string preText)
        {
            this.preText = preText;
            this.parentpath = parentpath;
            this.nestLevel = nestLevel;
            this.divIndex = divIndex;
            this.node = node;
        }

        public string ParentPath
        {
            get { return parentpath; }
        }

        public int NestLevel
        {
            get { return nestLevel; }
        }


        public int DivIndex
        {
            get { return divIndex; }
        }

        public string Divid
        {
            get
            {
                string rawdiv = "<div " + node.RawAttribute + "/>";
                try
                {
                    return XmlNodeController.Instance.DeserializeToNode(rawdiv).Properties["id"];
                }
                catch
                {
                    return null;
                }
            }
        }

        public string Divclass
        {
            get
            {
                string rawdiv = "<div " + node.RawAttribute + "/>";
                try
                {
                    return XmlNodeController.Instance.DeserializeToNode(rawdiv).Properties["class"];
                }
                catch
                {
                    return null;
                }
            }
        }

        public string Content
        {
            get { return node.Serialize(true); }
        }

        public string Text
        {
            get
            {
                return preText + node.Text;
            }
        }

        public IHtmlDivCollection Divs
        {
            get
            {
                return new HtmlDivCollection(node.Nodes,
                    string.Format("{0}{1}{2}", parentpath, DivIndex, StringHelper.default_html_pathspliter),
                    nestLevel + 1,
                    preText + node.Text);
            }
        }

        public IHtmlNode HtmlNode
        {
            get { return node; }
        }

        public string Serialize()
        {
            FormatStringBuilder builder = new FormatStringBuilder();
            for (int i = 0; i < nestLevel * 3; i++)
                builder.Indent();

            builder.Indent();
            builder.Indent();

            builder.AppendLine(string.Format("<div path='{0}{1}' nest='{2}' class='{3}' id='{4}'>",
                parentpath, divIndex, nestLevel, Divclass, Divid));
            if (Divs.Count > 0)
            {
                builder.Append(Divs.Serialize());
            }
            else
            {
                builder.Indent();
                builder.AppendLine(string.Format("<content>{0}</content>", node.Serialize()));
                builder.Outdent();
            }
            builder.AppendLine("</div>");
            builder.Outdent();
            builder.Outdent();

            for (int i = 0; i < nestLevel * 3; i++)
                builder.Outdent();

            return builder.ToString();
        }
    }
}
